/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.cxhd.lis.DataCache;
import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.ToolDateTime;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class LisSamplecollection extends Model<LisSamplecollection> {

    public static final LisSamplecollection lisSqdDao = new LisSamplecollection();

    public String insertSample(Controller con, String req, String infopk) throws Exception {
        Date thisdate = new Date(System.currentTimeMillis());
        java.sql.Timestamp thisTime = new java.sql.Timestamp(thisdate.getTime());
        set("PK", getPk())
                .set("STAYHOSPITALMODE", con.getPara("lx"))
                .set("INFOPK", infopk)
                .set("BARCODE", getCode())
                .set("YBLB", "1")
                .set("REQUESTTIME", thisTime)
                .set("REQUESTER", req)
                .set("SAMPLE", con.getPara("yb"))
                .set("CYSJ", thisTime)
                .set("SECTION", con.getPara("ksmc"))
                .set("BED_NO", con.getPara("ch"))
                .set("EXAMINAIM", con.getPara("xmid"))
                .save();
        return get("PK") + "";
    }

    public void updateInfo(Controller con) throws Exception {
        findById(con.getPara("pk"))
                .set("STAYHOSPITALMODE", con.getPara("lx"))
                .set("SECTION", con.getPara("ksmc"))
                .set("BED_NO", con.getPara("ch"))
                .set("EXAMINAIM", con.getPara("xmm"))
                .set("SAMPLE", con.getPara("yb"))
                .update();
    }
//     public void updateXX(Controller con) throws Exception{
//        findById(con.getPara("samplepk"))
//                .set("SAMPLE",con.getPara("sample"))
//                .set("STAYHOSPITALMODE",con.getPara("STAYHOSPITALMODE"))
//                .set("SECTION",con.getPara("ks"))
//                .set("BED_NO",con.getPara("ch"))
//                .set("DIAGNOSTIC",con.getPara("lczd"))
//                .update();
//    }

    public void updateResult(String PKS, String name) throws Exception {

        String sql = "update lis_samplecollection set RESULTSTATUS='2',CHECKOPERATOR='" + name + "',CHECKTIME=NOW() WHERE PK IN (" + PKS + ") ";
        Db.update(sql);
    }

    public String saveSampleCollection(Controller con, String req, String sampleNo, String uuid, List<Map<String, String>> uuids) throws Exception {
        LisPatient lp = new LisPatient();
        Map<String, String> patInfo = new Gson().fromJson(con.getPara("patInfo"), new TypeToken<Map<String, String>>() {
        }.getType());

        System.out.println(patInfo);
        String PATIENTID = patInfo.get("PATIENTID");
        String samplepk = patInfo.get("samplepk");
        String infopk = patInfo.get("PK");

        // if (PATIENTID == null) {//判断是否是条码病人
        infopk = lp.saveInfo(patInfo);
        // System.out.println(infopk + "------@@@@@@@@@@--------");
        // } else {

        //}
        if (uuids != null && !uuids.isEmpty()) {
            updateSample(patInfo, uuids, infopk, req);

        } else {
            updateSample(patInfo, uuid, sampleNo, infopk, req);
//        if (samplepk == null) {
//            set("PK", getPk())
//                    .set("STAYHOSPITALMODE", patInfo.get("STAYHOSPITALMODE"))
//                    .set("UUID", uuid)
//                    .set("INFOPK", infopk)
//                    .set("SAMPLENO", sampleNo)
//                     .set("BARCODE", patInfo.get("BARCODE"))
//                    .set("RECEIVER", req)
//                    .set("RECEIVETIME", DateUtil.getDateEN())
//                    .set("RESULTSTATUS", "1")
//                    .set("SECTION", patInfo.get("SECTION"))
//                    .set("BED_NO", patInfo.get("BED_NO"))
//                    .set("EXAMINAIM", patInfo.get("EXAMINAIM"))
//                    .set("SAMPLE", patInfo.get("SAMPLE"))
//                    .set("DIAGNOSTIC", patInfo.get("DIAGNOSTIC"))
//                    .set("SJYS", patInfo.get("SJYS"))
//                    .set("JYYS", patInfo.get("JYYS"))
//                    .save();
//        } else {
//            findById(samplepk)
//                    .set("STAYHOSPITALMODE", patInfo.get("STAYHOSPITALMODE"))
//                    .set("UUID", uuid)
//                    .set("INFOPK", infopk)
//                     .set("BARCODE", patInfo.get("BARCODE"))
//                    .set("SAMPLENO", sampleNo)
//                    .set("RECEIVER", req)
//                    .set("RECEIVETIME", DateUtil.getDateEN())
//                    .set("RESULTSTATUS", "1")
//                    .set("SECTION", patInfo.get("SECTION"))
//                    .set("BED_NO", patInfo.get("BED_NO"))
//                    .set("EXAMINAIM", patInfo.get("EXAMINAIM"))
//                    .set("SAMPLE", patInfo.get("SAMPLE"))
//                    .set("DIAGNOSTIC", patInfo.get("DIAGNOSTIC"))
//                    .set("SJYS", patInfo.get("SJYS"))
//                    .set("JYYS", patInfo.get("JYYS"))
//                    .update();
//
//        }
        }
        return infopk;
    }

    public void updateSample(Map<String, String> patInfo, List<Map<String, String>> uuids, String infopk, String req) {

        for (Map<String, String> map : uuids) {
            String sql = "replace into lis_samplecollection "
                    + "set STAYHOSPITALMODE=?,"
                    + " UUID=? ,"
                    + " INFOPK=?,"
                    + " SAMPLENO=? ,"
                    + " RECEIVER=? ,"
                    + " RECEIVETIME=?,"
                    + " RESULTSTATUS=?,"
                    + " SECTION=?,"
                    + " BED_NO=?,"
                    + " EXAMINAIM=?,"
                    + " SAMPLE=?,"
                    + " DIAGNOSTIC=?,"
                    + " SJYS=?,"
                    + " JYYS=?,"
                    + " SELF_TYPE=?,"
                    + " ROOM_TEMPERATURE=?,"
                    + " REAGENT=?,"
                     + " CUSTOM1=?,"
                       + " CUSTOM2=?"
                    + "";

            // System.out.println(sql);
            Db.update(sql, patInfo.get("STAYHOSPITALMODE"), map.get("uuid"), infopk, map.get("sampleNo"), req, DateUtil.getDateEN(), "1", patInfo.get("SECTION"), patInfo.get("BED_NO"), patInfo.get("EXAMINAIM"), patInfo.get("SAMPLE"), patInfo.get("DIAGNOSTIC"), patInfo.get("SJYS"), patInfo.get("JYYS"), patInfo.get("SELF_TYPE"), patInfo.get("ROOM_TEMPERATURE"), patInfo.get("REAGENT"), patInfo.get("CUSTOM1"),patInfo.get("CUSTOM2"));
        }

    }

    public void updateSample(Map<String, String> patInfo, String uuid, String sample_no, String infopk, String req) {

        String sql = "replace into lis_samplecollection "
                + "set STAYHOSPITALMODE=?,"
                + " UUID=? ,"
                + " INFOPK=?,"
                + " SAMPLENO=? ,"
                + " RECEIVER=? ,"
                + " RECEIVETIME=?,"
                + " RESULTSTATUS=?,"
                + " SECTION=?,"
                + " BED_NO=?,"
                + " EXAMINAIM=?,"
                + " SAMPLE=?,"
                + " DIAGNOSTIC=?,"
                + " SJYS=?,"
                + " JYYS=?,"
                + " BARCODE=?,"
                + " SELF_TYPE=?,"
                + " ROOM_TEMPERATURE=?,"
                + " REAGENT=?,"
                + " CUSTOM1=?,"
                  + " CUSTOM2=?"
                + "";

        //System.out.println(sql);
        Db.update(sql, patInfo.get("STAYHOSPITALMODE"), uuid, infopk, sample_no, req, DateUtil.getDateEN(), "1", patInfo.get("SECTION"), patInfo.get("BED_NO"), patInfo.get("EXAMINAIM"), patInfo.get("SAMPLE"), patInfo.get("DIAGNOSTIC"), patInfo.get("SJYS"), patInfo.get("JYYS"), patInfo.get("BARCODE"), patInfo.get("SELF_TYPE"), patInfo.get("ROOM_TEMPERATURE"), patInfo.get("REAGENT"),patInfo.get("CUSTOM1"),patInfo.get("CUSTOM2"));

    }

    public void updateSample(Controller con, String parentPk, String req) {
        Map<String, String> patInfo = new Gson().fromJson(con.getPara("patInfo"), new TypeToken<Map<String, String>>() {
        }.getType());
        findById(parentPk)
                .set("STAYHOSPITALMODE", patInfo.get("STAYHOSPITALMODE"))
                .set("UUID", con.getPara("UUID"))
                .set("SAMPLENO", con.getPara("sampleNo"))
                .set("RECEIVER", req)
                .set("RECEIVETIME", DateUtil.getDateEN())
                .set("RESULTSTATUS", "1")
                .set("SECTION", patInfo.get("SECTION"))
                .set("BED_NO", patInfo.get("BED_NO"))
                .set("EXAMINAIM", patInfo.get("EXAMINAIM"))
                .set("SAMPLE", patInfo.get("SAMPLE"))
                .set("DIAGNOSTIC", patInfo.get("DIAGNOSTIC"))
                .set("SJYS", patInfo.get("SJYS"))
                .set("JYYS", patInfo.get("JYYS"))
                .set("SELF_TYPE", patInfo.get("SELF_TYPE"))
                .set("ROOM_TEMPERATURE", patInfo.get("ROOM_TEMPERATURE"))
                .set("REAGENT", patInfo.get("REAGENT"))
                .set("CUSTOM1", patInfo.get("CUSTOM1"))
                .set("CUSTOM2", patInfo.get("CUSTOM2"))
                .update();

    }

    public void updateXX(Controller con) throws Exception {
        findById(con.getPara("brpk"))
                .set("SJYS", con.getPara("sjys"))
                .set("JYYS", con.getPara("jyys"))
                .update();
    }

    public void updateDY(Controller con) throws Exception {
        String time = con.getPara("time");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        java.util.Date date = sdf.parse(time);
        java.sql.Timestamp stp = new java.sql.Timestamp(date.getTime());
        findById(con.getPara("pk"))
                .set("PRINTTIME", DateUtil.getDateEN())
                .set("RESULTSTATUS", "3")
                .update();
    }
    
      public void updateDY(String uuid) throws Exception {
      
     
       Db.update("update lis_samplecollection set PRINTTIME='"+DateUtil.getDateEN()+"',RESULTSTATUS='3' where UUID='"+uuid+"'");
              
    }

    public void deleteInfo(Controller con) throws Exception {
        if (con.getPara("pk") != null) {
            deleteById(con.getPara("pk"));
        }
    }

    public String getPk() {
        List<Record> list = Db.use("mysql").find("select * from lis_samplecollection limit 0,10");
        String value = "1";
        if (list.size() > 0) {
            value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(PK) as id from lis_samplecollection").get("id") + "") + 1) + "";
        }
        return value;
    }

    public String getCode() {
        List<Record> list = Db.use("mysql").find("select BARCODE from lis_samplecollection");
        String code = "";
        String brcode = "";

        if (DataCache.getIntance().getConfig().get("brcodeStartChar") != null) {
            code = DataCache.getIntance().getConfig().get("brcodeStartChar");
        }
        String value = "016801";
        try {
            if (list != null || list.size() > 0) {
                value = (Long.parseLong(Db.use("mysql").findFirst("SELECT MAX(BARCODE) as id from lis_samplecollection").get("id") + "") + 1) + "";
                brcode = value;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!code.equalsIgnoreCase("")) {
            String code2 = (value + "").substring(0, 4);
            System.out.println(code2 + "------" + code2.equalsIgnoreCase(code));
            if (code2.equalsIgnoreCase(code)) {
                brcode = value;
            } else {
                brcode = code + value;
            }
        }
        System.out.println(brcode + "-----");
        return brcode;
    }
}
